package mk.com.ldm.corepoint.implementation.client.lib.wrappers.filters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import mk.com.ldm.corepoint.implementation.client.lib.generators.BaseFilter;
import mk.com.ldm.corepoint.implementation.shared.Filter;
import mk.com.ldm.corepoint.implementation.shared.TableColumn;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

public class DateRangeFilter extends BaseFilter{

	public DateRangeFilter(final HashMap<String, Filter> filters, final TableColumn tc, Button filterBtn) {
		super(filters, tc, filterBtn);
		
		VerticalPanel vp = new VerticalPanel();
		final HashMap<String, String> hm = new HashMap<String, String>();
		final Filter f = new Filter(tc.getHeader(), hm, tc.getFilterOperator());
		final DateBox from = new DateBox();
		from.setWidth((tc.getWidth()-22)+"px");
		final DateBox to = new DateBox();
		to.setWidth((tc.getWidth()-10)+"px");
		
		final String dateFormat = tc.getFilterField().split(",")[2];
//		MessageBox.info("", dateFormat);
		from.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(dateFormat)));
		to.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(dateFormat)));
		//new SimpleDateFormat(dateFormat).format(date)
		
		from.addValueChangeHandler(new ValueChangeHandler<Date>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				if(from.getValue()!=null||!from.getValue().toString().isEmpty()){
					hm.put("from", new SimpleDateFormat(dateFormat).format(from.getValue()));
					if(to.getValue()==null||to.getValue().toString().isEmpty()){
						to.setValue(new Date());
						hm.put("to", new SimpleDateFormat(dateFormat).format(to.getValue()));
					}
					filters.put(tc.getHeader(), f);
				}
				else{
					if(!hm.get("from").isEmpty()){
						hm.remove("from");
					}
					if(hm.size()==0){
						filters.remove(tc.getHeader());
					}
				}
			}
		});
		
		to.addValueChangeHandler(new ValueChangeHandler<Date>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				if(to.getValue()!=null||!to.getValue().toString().isEmpty()){
					hm.put("to", new SimpleDateFormat(dateFormat).format(to.getValue()));
					if(from.getValue()==null||from.getValue().toString().isEmpty()){
						Date fromDate = new Date();
						fromDate.setYear(10);
						//MessageBox.info("", new Date().getTime()+"");
						from.setValue(fromDate);
						hm.put("from", new SimpleDateFormat(dateFormat).format(from.getValue()));
					}
					filters.put(tc.getHeader(), f);
				}
				else{
					if(!hm.get("to").isEmpty()){
						hm.remove("to");
					}
					if(hm.size()==0){
						filters.remove(tc.getHeader());
					}
				}
			}
		});
		
		Label fromLbl = new Label("from:");
		fromLbl.setStyleName("date-range-labels");
		HorizontalPanel h1 = new HorizontalPanel();
		h1.add(fromLbl);
		h1.add(from);
		vp.add(h1);
//		vp.add(new Label("to:"));
		HorizontalPanel h2 = new HorizontalPanel();
		Label toLbl = new Label("to:");
		toLbl.addStyleName("date-range-labels");
		h2.add(toLbl);
		h2.add(to);
		vp.add(h2);
		vp.addStyleName("date-range-filter");
		
		initWidget(vp);
	}

}
